/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.domain.AdministrativeOffice;
import com.oyun.modules.medicine.domain.Hospital;
import com.oyun.modules.medicine.domain.vo.AdministrativeOfficeQueryCriteria;
import com.oyun.modules.medicine.mapper.AdministrativeOfficeMapper;
import com.oyun.modules.medicine.mapper.HospitalMapper;
import com.oyun.modules.medicine.service.AdministrativeOfficeService;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author aoyun
 * @description 服务实现
 * @date 2024-04-28
 **/
@Service
@RequiredArgsConstructor
public class AdministrativeOfficeServiceImpl extends ServiceImpl<AdministrativeOfficeMapper, AdministrativeOffice> implements AdministrativeOfficeService {

    private final AdministrativeOfficeMapper administrativeOfficeMapper;

    private final HospitalMapper hospitalMapper;

    @Override
    public PageResult<AdministrativeOffice> queryAll(AdministrativeOfficeQueryCriteria criteria, Page<Object> page) {
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        IPage<AdministrativeOffice> all = administrativeOfficeMapper.findAll(criteria, page);
        for (AdministrativeOffice administrativeOffice : all.getRecords()) {
            LambdaQueryWrapper<Hospital> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Hospital::getId, administrativeOffice.getHpId());
            Hospital hospital = hospitalMapper.selectOne(wrapper);
            if (hospital != null) {
                administrativeOffice.setHpName(hospital.getMnName());
            }
        }
        return PageUtil.toPage(all);
    }

    @Override
    public List<AdministrativeOffice> queryAll(AdministrativeOfficeQueryCriteria criteria) {
        return administrativeOfficeMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AdministrativeOffice resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AdministrativeOffice resources) {
        AdministrativeOffice administrativeOffice = getById(resources.getId());
        administrativeOffice.copy(resources);
        convertCode(administrativeOffice);
        saveOrUpdate(administrativeOffice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<AdministrativeOffice> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AdministrativeOffice administrativeOffice : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("医院", administrativeOffice.getHpId());
            map.put("蒙语名称", administrativeOffice.getMnName());
            map.put(" mnNameCode", administrativeOffice.getMnNameCode());
            map.put(" zhName", administrativeOffice.getZhName());
            map.put("所属大类", administrativeOffice.getMnType());
            map.put(" mnTypeCode", administrativeOffice.getMnTypeCode());
            map.put(" zhType", administrativeOffice.getZhType());
            map.put("电话", administrativeOffice.getPhone());
            map.put("科室地址", administrativeOffice.getMnAddress());
            map.put(" mnAddressCode", administrativeOffice.getMnAddressCode());
            map.put(" zhAddress", administrativeOffice.getZhAddress());
            map.put("科室特色(字典)", administrativeOffice.getCharacteristics());
            map.put("科室负责人", administrativeOffice.getMnPerson());
            map.put(" mnPersonCode", administrativeOffice.getMnPersonCode());
            map.put(" zhPerson", administrativeOffice.getZhPerson());
            map.put("介绍", administrativeOffice.getMnIntroduce());
            map.put(" mnIntroduceCode", administrativeOffice.getMnIntroduceCode());
            map.put(" zhIntroduce", administrativeOffice.getZhIntroduce());
            map.put(" createBy", administrativeOffice.getCreateBy());
            map.put(" createTime", administrativeOffice.getCreateTime());
            map.put(" updateBy", administrativeOffice.getUpdateBy());
            map.put(" updateTime", administrativeOffice.getUpdateTime());
            map.put(" status", administrativeOffice.getStatus());
            map.put("备注", administrativeOffice.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<AdministrativeOffice> getNameAndId() {
        LambdaQueryWrapper<AdministrativeOffice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdministrativeOffice::getStatus, 1);
        return administrativeOfficeMapper.selectList(wrapper);
    }

    @Override
    public Page<AdministrativeOffice> getOfficeByHpId(Long hpId, Long pageNum, Long pageSize) {

        Page<AdministrativeOffice> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AdministrativeOffice> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(AdministrativeOffice::getStatus, 1).eq(AdministrativeOffice::getHpId, hpId);
       // List<AdministrativeOffice> administrativeOffices = administrativeOfficeMapper.selectList(page,wrapper1);
        Page<AdministrativeOffice> administrativeOffices = administrativeOfficeMapper.selectPage(page, wrapper1);
        for (AdministrativeOffice administrativeOffice : administrativeOffices.getRecords()) {
            LambdaQueryWrapper<Hospital> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Hospital::getId, administrativeOffice.getHpId());
            Hospital hospital = hospitalMapper.selectOne(wrapper);
            if (hospital != null) {
                administrativeOffice.setHpName(hospital.getMnName());
            }
        }
        return administrativeOffices;
    }

    @Override
    public Object getByUsername(String name) {
        String replaceAll = IntermediateCodeUtils.mn2Code(name).replaceAll("-", " -");
        LambdaQueryWrapper<AdministrativeOffice> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(AdministrativeOffice::getZhName, name).or().like(AdministrativeOffice::getMnNameCode, replaceAll);
        List<AdministrativeOffice> list = administrativeOfficeMapper.selectList(wrapper);
        for (AdministrativeOffice administrativeOffice : list) {
            LambdaQueryWrapper<Hospital> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Hospital::getId, administrativeOffice.getHpId());
            Hospital hospital = hospitalMapper.selectOne(wrapper1);
            administrativeOffice.setHpName(hospital.getMnName());
        }
        return list;
    }

    private void convertCode(AdministrativeOffice administrativeOffice) {
        administrativeOffice.setMnNameCode(IntermediateCodeUtils.mn2Code(administrativeOffice.getMnName()).replaceAll("-", " -"));
        administrativeOffice.setMnTypeCode(IntermediateCodeUtils.mn2Code(administrativeOffice.getMnType()).replaceAll("-", " -"));
        administrativeOffice.setMnAddressCode(IntermediateCodeUtils.mn2Code(administrativeOffice.getMnAddress()).replaceAll("-", " -"));
        administrativeOffice.setMnPersonCode(IntermediateCodeUtils.mn2Code(administrativeOffice.getMnPerson()).replaceAll("-", " -"));
        administrativeOffice.setMnIntroduceCode(IntermediateCodeUtils.mn2Code(administrativeOffice.getMnIntroduce()).replaceAll("-", " -"));
    }
}
